<?php

namespace app\api\controller\fruit\v22;

use app\api\model\fruit\warrior\User;
use think\cache\driver\Redis;

/**
 * @author zero
 * @Time 2019/2/25
 *
 * Class Warriorendless
 * @package app\api\controller\fruit\v21
 *
 * 无尽模式相关接口 v21
 */
class Warriorendless extends Warrior
{

    // 额外获取入场券方式数量上限
    public $can_num = ['video' => 2, 'share' => 1];

    // 无尽模式每个水果分值
    public $pre_fruit_score = 2;

    // 获取无尽模式展示数据
    public function get_info()
    {
        $data['ticket_num'] = $this->user->endless_ticket_num;

        $redis = new Redis();
        $data['can_video_num'] = 2 - $redis->get($this->user->openid . '_endless_use_video_num', 0);
        $data['can_share_num'] = 1 - $redis->get($this->user->openid . '_endless_use_share_num', 0);
        $list = User::order('endless_score', 'desc')->limit(5)->select();

        foreach ($list as $item) {
            $data['rank'][] = ['nick_name' => $item->nick_name, 'avatar_url' => $item->avatar_url, 'score' => $item->endless_score];
        }

        $this->result('info', $data);
    }

    // 增加入场券
    public function add_ticket()
    {
        $type = $this->request->param('type');
        $end_day_time = strtotime(date('Y-m-d', strtotime('+1 day'))) - time();
        $redis = new Redis();
        // 获取已经使用的次数
        $use_num = $redis->get($this->user->openid . '_endless_use_' . $type . '_num', 0);
        // 对比已使用和能使用
        if ($use_num < $this->can_num[$type]) {
            $use_num += 1;
            $redis->set($this->user->openid . '_endless_use_' . $type . '_num', $use_num, $end_day_time);
            $this->user->endless_ticket_num += 1;
            if ($this->user->save() !== false) {
                if ($type == 'video') {
                    // 处理看视频成就
                    $this->handle_achieve('video_num');
                }
                $this->result('ok');
            }
        }
        $this->result('fail');
    }

    // 开始游戏,扣除入场券操作
    public function enter_endless()
    {
        if ($this->user->endless_ticket_num >= 1) {
            $this->user->endless_ticket_num -= 1;
            if ($this->user->save() !== false) {
                $this->result('ok');
            }
        }
        $this->result('fail');
    }

    // 结算
    public function balance()
    {
        $num = $this->request->param('num');
        $score = $num * $this->pre_fruit_score;
        $this->user->endless_score += $score;
        $this->user->save();

        $rank = -1;
        $not_score = 0;
        $five = User::order('endless_score', 'desc')->limit(4,1)->select();
        if (count($five) > 0) {
            $five = $five[0];
            $not_score = $five->endless_score - $this->user->endless_score;
            if ($not_score <= 0) {
                $rank = 0;
                $not_score = 0;
            } else {
                $rank = 5;
            }
        }

        $data['score'] = $score;
        $data['all_score'] = $this->user->endless_score;
        $data['rank'] = $rank;
        $data['not_score'] = $not_score;
        $this->result('ok', $data);
    }

    // 每周中奖者数据
    public function get_week_lucky()
    {
        $redis = new Redis();
        $arr = $redis->get('warrior_fruit_week_lucky');
        if ($arr) {
            $arr = json_decode($arr, true);
        } else {
            $list = User::whereIn('week_lucky', '1,2,3,4,5')->select();
            foreach ($list as $key => $item) {
                $arr[] = ['nick_name' => $item->nick_name, 'avatar_url' => $item->avatar_url, 'phone_number' => $item->phone_number];
            }
            $redis->set('warrior_fruit_week_lucky', json_encode($arr));
        }
        $this->result('list', $arr);
    }

    // 中奖者跑马灯数据
    public function get_lucky_marquee()
    {
        $redis = new Redis();
        $arr = $redis->get('warrior_fruit_week_lucky');
        if ($arr) {
            $arr = json_decode($arr, true);
            foreach ($arr as $item) {
                $marquee[] = $item['phone_number'];
            }
        } else {
            $marquee = User::whereIn('week_lucky', '1,2,3,4,5')->column('phone_number');
        }
        $this->result('ok', $marquee);
    }

}
